package com.tjf.lib_utils;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.PopupWindow;

/**
 * 使用
 * PopupWindowHelper popupHelper = new PopupWindowHelper();
 * popupHelper.showPopupWindow(context, anchorView);
 * <p> 位置方向
 * // 当需要关闭PopupWindow时
 * popupHelper.dismissPopupWindow();
 */
public class PopupWindowHelper {
    private PopupWindow popupWindow;

    public void showPopupWindow(Context context, View anchorView, boolean positionDirection,
                                View popupView, int[] whs, PopupWindow.OnDismissListener onDismissListener) {
        // 使用LayoutInflater来加载popup布局
//        View popupView = LayoutInflater.from(context).inflate(layoutId, null);
        // 创建PopupWindow实例
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(PxUtils.dp2px(context, whs[0]));
        popupWindow.setHeight(PxUtils.dp2px(context, whs[1]));
        // 设置PopupWindow可以获得焦点，否则无法响应点击事件
        popupWindow.setFocusable(true);
        /* popupWindow.setOutsideTouchable(false);*/
        popupWindow.setBackgroundDrawable(new ColorDrawable()); // 必须设置Background才能消失
        // 获取view的位置
        int[] location = new int[2];
        anchorView.getLocationOnScreen(location);
        int x; // 水平居中
        int y; // 在view的上方显示
        if (positionDirection) {//上方显示
            x = location[0] + (anchorView.getWidth() - popupWindow.getWidth()) / 2; // 水平居中
            y = location[1] - popupWindow.getHeight(); // 在view的上方显示
        } else {//下方显示
            x = location[0];
            y = location[1] + anchorView.getHeight(); // 在anchorView的下方显示
        }
// 计算PopupWindow的显示位置
        LogsUtils.i("位置高度location00==", location[0], location[1]);
        // 显示PopupWindow
        popupWindow.showAtLocation(anchorView, Gravity.TOP | Gravity.CENTER, 0, y);
        if (onDismissListener != null) {
            popupWindow.setOnDismissListener(onDismissListener);
        }
    }

    public void showPopupWindowUp(Context context, View anchorView,
                                  View popupView, int[] whs, PopupWindow.OnDismissListener onDismissListener) {
        // 创建PopupWindow实例
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(PxUtils.dp2px(context, whs[0]));
//        popupWindow.setHeight(PxUtils.dp2px(context, whs[1]));
        // 设置PopupWindow可以获得焦点，否则无法响应点击事件
        popupWindow.setFocusable(true);
        /* popupWindow.setOutsideTouchable(false);*/
//        popupWindow.setBackgroundDrawable(new ColorDrawable()); // 必须设置Background才能消失
        // 获取view的位置

        int navigationBarHeight = ShareUtils.getInstance().getInt("NavigationBarHeight");
        // 显示PopupWindow
        popupWindow.showAtLocation(anchorView, Gravity.BOTTOM | Gravity.CENTER,
                0, PxUtils.dp2px(context,  navigationBarHeight==0
                        ?84:navigationBarHeight<=50?navigationBarHeight+60:navigationBarHeight));
        if (onDismissListener != null) {
            popupWindow.setOnDismissListener(onDismissListener);
        }
    }

    public boolean isNavBarVisible(Window context) {
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        context.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = metrics.heightPixels;
        return realHeight > usableHeight;
    }

    public void showPopupWindowUpDown(Context context, int[] whs, View anchorView,
                                      View popupView, PopupWindow.OnDismissListener onDismissListener) {
        // 创建PopupWindow实例
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(PxUtils.dp2px(context, whs[0]));
        popupWindow.setHeight(PxUtils.dp2px(context, whs[1]));
        // 设置PopupWindow可以获得焦点，否则无法响应点击事件
        popupWindow.setFocusable(true);
        /* popupWindow.setOutsideTouchable(false);*/
        popupWindow.setBackgroundDrawable(new ColorDrawable()); // 必须设置Background才能消失
        // 获取view的位置


        // 显示PopupWindow
        popupWindow.showAtLocation(anchorView, Gravity.BOTTOM | Gravity.CENTER,
                0, 0);
        if (onDismissListener != null) {
            popupWindow.setOnDismissListener(onDismissListener);
        }
    }

    public void showPopupWindow(Context context, View anchorView,
                                View popupView, int[] whs, PopupWindow.OnDismissListener onDismissListener) {
        // 创建PopupWindow实例
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(PxUtils.dp2px(context, whs[0]));
        popupWindow.setHeight(PxUtils.dp2px(context, whs[1]));
        // 设置PopupWindow可以获得焦点，否则无法响应点击事件
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable()); // 必须设置Background才能消失
        // 显示PopupWindow
        popupWindow.showAsDropDown(anchorView);
        if (onDismissListener != null) {
            popupWindow.setOnDismissListener(onDismissListener);
        }
    }

    public void upDateUi(int width, int height) {
        popupWindow.update(width, height);
    }

    public void upDateUi(Context context, View anchorView, int width, int height, int y) {

//        popupWindow.update(anchorView, -(PxUtils.dp2px(context, 40)), -y, width, height);
    }

    public void dismissPopupWindow() {

        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }
}